package com.gemme.gemme.pop;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;

import java.text.DecimalFormat;

/**
 * @author falcon
 * @Description: PopupWindow核心功能
 */
public abstract class CorePopUtil {

    protected OnDismissListener onDismissListener;
    protected PopupWindow popupWindow;
    protected Context mContext;
    protected View mView;

    public CorePopUtil(View view, Context context, int layout) {
        this.mView = view;

        mView = LayoutInflater.from(context).inflate(layout, null);
        popupWindow = new PopupWindow(mView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

        popupWindow.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                popupWindow.dismiss();

                if (onDismissListener != null) {
                    onDismissListener.onDismiss();
                }
            }
        });

        this.mContext = context;
        context = null;
    }

    public abstract void initLayout(View view, Context context);

    /**
     * 下拉式弹出pop菜单 parent 右下角
     */
    public void showAsDropDown(View view) {
        if (Build.VERSION.SDK_INT >= 24) {
            Rect visibleFrame = new Rect();
            view.getGlobalVisibleRect(visibleFrame);
            int height = view.getResources().getDisplayMetrics().heightPixels - visibleFrame.bottom;
            popupWindow.setHeight(height);
            popupWindow.showAsDropDown(view, 0, 0);
        } else {
            popupWindow.showAsDropDown(view, 0, 0);

        }
        // 这个是为了点击“返回Back”也能使其消失
        popupWindow.setBackgroundDrawable(new ColorDrawable(0000000000));
        // 设置允许在外点击消失
        popupWindow.setOutsideTouchable(false);
        // 使其聚集
        popupWindow.setFocusable(true);
        // 设置PopupWindow可触摸
        popupWindow.setTouchable(true);
        // 设置弹出位置
        popupWindow.showAsDropDown(view);
        // 刷新状态
        popupWindow.update();
    }

    public void showPopUp(View view) {
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new ColorDrawable(0000000000));

        int[] location = new int[2];
        view.getLocationOnScreen(location);

        popupWindow.showAtLocation(view, Gravity.TOP, 15, location[1] - popupWindow.getHeight());
        popupWindow.update();
    }

    /**
     * 下拉式弹出 pop菜单 parent 右下角
     */
    public void showAsDropDownInstance() {
        // 设置允许在外点击消失
        popupWindow.setOutsideTouchable(false);
        // 使其聚集
        popupWindow.setFocusable(true);
        // 设置PopupWindow可触摸
        popupWindow.setTouchable(true);
        // 设置弹出位置
        popupWindow.showAtLocation(mView, Gravity.CENTER, 0, 0);
        // 刷新状态
        popupWindow.update();
    }

    /**
     * 下拉式弹出 pop菜单 parent 右下角
     */
    public void showAtLocation() {
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        // 设置允许在外点击消失
        popupWindow.setOutsideTouchable(true);
        // 使其聚集
        popupWindow.setFocusable(true);
        // 设置PopupWindow可触摸
        popupWindow.setTouchable(true);
        popupWindow.setBackgroundDrawable(new ColorDrawable(0000000000));
        popupWindow.setTouchInterceptor(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                    popupWindow.dismiss();
                    return true;
                }
                return false;
            }
        });
        // 设置弹出位置
        popupWindow.showAtLocation(mView, Gravity.CENTER, 0, 0);
        // 刷新状态
        popupWindow.update();
    }

    /**
     * 下拉式弹出 pop菜单 parent 右下角
     */
    public void showAtLocation(boolean update) {
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        // 设置允许在外点击消失
        popupWindow.setOutsideTouchable(true);
        // 使其聚集
        popupWindow.setFocusable(true);
        // 设置PopupWindow可触摸
        popupWindow.setTouchable(true);
        popupWindow.setBackgroundDrawable(new ColorDrawable(0000000000));
        popupWindow.setTouchInterceptor(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                    popupWindow.dismiss();
                    return true;
                }
                return false;
            }
        });
        // 设置弹出位置
        popupWindow.showAtLocation(mView, Gravity.CENTER, 0, 0);

        if (update) {
            // 刷新状态
            popupWindow.update();
        }
    }

    /**
     * 下拉式 弹出 pop菜单 parent 右下角
     */
    public void showAtBottom() {
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        // 设置允许在外点击消失
        popupWindow.setOutsideTouchable(true);
        // 使其聚集
        popupWindow.setTouchable(true);
        popupWindow.setFocusable(true);
        popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setTouchInterceptor(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                    popupWindow.dismiss();
                    return true;
                }
                return false;
            }
        });
        popupWindow.update();
    }

    /**
     * 判断Pop是否弹出
     */
    public boolean isShow() {
        return popupWindow.isShowing();
    }

    /**
     * 隐藏菜单
     */
    public void dismissPop() {
        popupWindow.dismiss();
    }

    /**
     * 是否强制
     *
     * @param focusable
     */
    public void setFocusable(boolean focusable) {
        popupWindow.setFocusable(focusable);
    }

    public void setOnDismissListener(OnDismissListener dismissListener) {
        this.onDismissListener = dismissListener;
    }
}